//
//  BaseTextView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/26.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class BaseTextView: UITextView {
    var backView:UIView?
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   init(changeView:UIView) {
    super.init(frame: CGRect.zero, textContainer: nil)
    backView = changeView
    NotificationCenter.default.addObserver(self, selector: #selector(notifierKeyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
    
    NotificationCenter.default.addObserver(self, selector: #selector(notifierKeyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    
    self.inputAccessoryView = self.generateToolbar()
    }
    
    func generateToolbar() -> UIToolbar {
        let toolbar =  UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: kSCREEN_WIDTH, height: 44.0))
        let itme1 = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.flexibleSpace , target: nil, action: nil)
        let item2 = UIBarButtonItem(title: "完成", style: UIBarButtonItemStyle.plain, target: self, action: #selector(doneButtonDidPressed))
        toolbar.setItems([itme1, item2], animated: false)
        return toolbar
    }
    
    @objc func doneButtonDidPressed() -> Void {
        self .resignFirstResponder()
    }
    
    @objc func notifierKeyboardWillShow(notification:NSNotification) {
        let firstResponder = UIResponder.firstResponder()
        let isEqual = firstResponder?.isEqual(self) ?? false
        if !isEqual {
            return
        }
        let userInfo = notification.userInfo as NSDictionary?
        
        let aValue = userInfo?.object(forKey: UIKeyboardFrameEndUserInfoKey)
        
        let keyboardSize = (aValue as AnyObject).cgRectValue
        
        let animationDuration =  (userInfo?.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! NSNumber).doubleValue
        
        let animationCurve = (userInfo?.object(forKey: UIKeyboardAnimationCurveUserInfoKey) as! NSNumber).intValue
        self.layoutIfNeeded()
        let oldfame = self.convert(self.bounds, to: UIApplication.shared.keyWindow)
        let screenHeight = isiPhoneX() ? kSCREEN_HEIGHT+88 : kSCREEN_HEIGHT+64
        let height = screenHeight - (oldfame.size.height) - (oldfame.origin.y) - (keyboardSize?.size.height)!-15
        
        if height < 0 {
            UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: UIViewKeyframeAnimationOptions(rawValue: UIViewKeyframeAnimationOptions.RawValue(animationCurve)), animations: {
                self.backView!.frame =  CGRect(x: (self.backView!.frame.origin.x), y: (self.backView!.frame.origin.y) + (height), width: (self.backView!.frame.size.width), height: self.backView!.frame.size.height)
                
            }) { (make) in
                
            }
            
        }
    }
    
    
    @objc func notifierKeyboardWillHide(notification:NSNotification) {
        let userInfo = notification.userInfo as NSDictionary?
        let animationDuration =  (userInfo?.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! NSNumber).doubleValue
        
        let animationCurve = (userInfo?.object(forKey: UIKeyboardAnimationCurveUserInfoKey) as! NSNumber).intValue
        
        UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: UIViewKeyframeAnimationOptions(rawValue: UIViewKeyframeAnimationOptions.RawValue(animationCurve)), animations: {
            self.backView!.frame =  CGRect(x: (self.backView!.frame.origin.x), y: 0, width: (self.backView?.frame.size.width)!, height: (self.backView?.frame.size.height)!)
            
        }) { (make) in
            
        }
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
